package com.demo.web;

import com.demo.service.StudentService;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String name = req.getParameter("name");
            String num = req.getParameter("num");
            String sex = req.getParameter("sex");
            String birthDay = req.getParameter("birthDay");

            Map<String, Object> map = new StudentService().register(username, password, num, name, sex, birthDay);

            ObjectMapper om = new ObjectMapper();
            String mapJson = om.writeValueAsString(map);
            resp.getWriter().println(mapJson);


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
